package com.bluefox.modules.system.controller;

import com.bluefox.aop.log.Log;
import com.bluefox.utils.PageUtil;
import com.bluefox.utils.SecurityUtils;
import com.bluefox.modules.system.criteria.MenuQueryCriteria;
import com.bluefox.modules.system.service.SysRoleService;
import com.bluefox.modules.system.service.SysUserService;
import com.bluefox.modules.system.service.dto.SysMenuDto;
import com.bluefox.modules.system.service.SysMenuService;
import com.bluefox.modules.system.service.dto.SysUserDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (SysMenu)表控制层
 *
 * @author sunfeng
 * @since 2020-04-17 15:01:44
 */
@RestController
@RequestMapping("/system/sysMenu")
public class SysMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService roleService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PreAuthorize("@el.check('system:menu:query')")
    @GetMapping("/{id}")
    public SysMenuDto selectOne(@PathVariable  Long id) {
        return this.sysMenuService.queryById(id);
    }


    /**
     * 新增菜单数据
     */
    @PreAuthorize("@el.check('system:menu:add')")
    @PostMapping
    public ResponseEntity add(@Validated @RequestBody SysMenuDto sysMenuDto) {

        return new ResponseEntity<>(sysMenuService.insert(sysMenuDto), HttpStatus.OK);
    }
    /**
     * 删除菜单
     */
    @PreAuthorize("@el.check('system:user:remove')")
    @DeleteMapping("/{menuId}")
    public ResponseEntity<Object> remove(@PathVariable Long menuId ) {
        sysMenuService.deleteById(menuId);

        return new ResponseEntity<>(HttpStatus.OK);
    }
    /**
     * 修改菜单数据
     */
    @PreAuthorize("@el.check('system:menu:edit')")
    @PutMapping
    public ResponseEntity changeStatus(@RequestBody SysMenuDto sysMenuDto) {

        return new ResponseEntity<>(sysMenuService.update(sysMenuDto), HttpStatus.OK);
    }

    @Log("查询菜单")
    @GetMapping("/list")
    @PreAuthorize("@el.check('system:menu:list')")
    public ResponseEntity<Object> query(MenuQueryCriteria criteria) throws Exception {
        List<SysMenuDto> menuDtoList = sysMenuService.queryAll(criteria, true);
        return new ResponseEntity<>(PageUtil.toPage(menuDtoList, menuDtoList.size()),HttpStatus.OK);
    }


    @GetMapping(value = "/build")
    public ResponseEntity<Object> buildMenus() {
        // 获取登录的用户
        SysUserDto user = sysUserService.findByName(SecurityUtils.getUsername());
        // 获取用户的菜单列表
        List<SysMenuDto> menuDtoList = sysMenuService.findByRoles(roleService.findByUsersId(user.getId()));
        // 将菜单列表转换成菜单组件树
        List<SysMenuDto> menuDtos = (List<SysMenuDto>) sysMenuService.buildTree(menuDtoList).get("content");
        return new ResponseEntity<>(sysMenuService.buildMenus(menuDtos), HttpStatus.OK);
    }

    /**
     * 获取简单菜单树 下拉列表
     */
    @GetMapping("/treeselect")
    public ResponseEntity<Object> treeselect() {

        List<SysMenuDto> menus = sysMenuService.findByPid(0L);
        return new ResponseEntity<>(sysMenuService.buildMenuTreeSelect(menus), HttpStatus.OK);
    }

    /**
     * 根据角色id 获取菜单列表 以及选中的菜单
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public ResponseEntity<Object> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysMenuDto> menus = sysMenuService.findByPid(0L);
        HashMap<Object, Object> map = new HashMap<>();
        //  菜单id数组
        Set<Long> collect = sysMenuService.selectMenuListByRoleId(roleId).stream().map(SysMenuDto::getId).collect(Collectors.toSet());
        map.put("checkedKeys", collect);
        map.put("menus", sysMenuService.getMenuTree(menus));
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


}
